package com.linln.component.thymeleaf.utility;

import com.linln.common.utils.ReflectUtils;
import com.linln.common.utils.SpringContextUtil;
import com.linln.modules.system.service.UserService;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class EntityUtils {
    public Object readField(Object entity, Object fieldName) {
        if (entity instanceof Map) {
            return ((Map) entity).get(fieldName);
        } else {
            return ReflectUtils.readField(entity, fieldName + "");
        }
    }

    /**
     * 获取指定用户的昵称
     *
     * @param userId
     * @return
     */
    public String getUsername(Object userId) {
        try {
            UserService userService = SpringContextUtil.getBean(UserService.class);
            return userService.getById(((Number) userId).longValue()).getNickname();
        } catch (Exception e) {
            return "";
        }
    }

    public String getVerstionTitle(Object versionId) {
        try {
            Object versionServiceImpl = SpringContextUtil.getBean("versionServiceImpl");
            Object ver = ClassUtils.getPublicMethod(versionServiceImpl.getClass(), "getById", Long.class).invoke(versionServiceImpl, versionId);
            return (String) ReflectUtils.readField(ver, "demand_title");
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取指定版本中涉及的所有的产品线名称
     *
     * @param versionId
     * @return
     */
    public String getProductionTitles(Object versionId) {
        try {
            Object versionServiceImpl = SpringContextUtil.getBean("versionServiceImpl");
            Object ret = ClassUtils.getPublicMethod(versionServiceImpl.getClass(), "getProductionTitles", Long.class).invoke(versionServiceImpl, versionId);
            return (String) ret;
        } catch (Exception e) {
            return "";
        }
    }

    public String getProductionTitleByAppId(Object appId) {
        try {
            Object versionServiceImpl = SpringContextUtil.getBean("versionAppServiceImpl");
            Object ret = ClassUtils.getPublicMethod(versionServiceImpl.getClass(), "getProductionTitle", Long.class).invoke(versionServiceImpl, appId);
            return (String) ret;
        } catch (Exception e) {
            return "";
        }
    }

    public boolean equals(Object one, Object another) {
        if (one == null && another == null) {
            return true;
        }
        if (one == null) {
            return false;
        }
        if (another instanceof Collection && ((Collection) another).size() > 0) {
            another = ((Collection) another).iterator().next();
        }
        return String.valueOf(one).equals(String.valueOf(another));
    }

    /**
     * 获取指定用户的昵称
     *
     * @param userIds
     * @return
     */
    public String getUsernames(List<?> userIds) {
        StringBuilder ret = new StringBuilder();
        if (userIds == null) {
            return ret.toString();
        }
        try {
            for (Object userId : userIds) {
                String username = getUsername(userId);
                if (StringUtils.isBlank(username)) {
                    continue;
                }
                if (ret.length() > 0) {
                    ret.append(", ");
                }
                ret.append(username);
            }
        } catch (Exception e) {
        }
        return ret.toString();
    }

    /**
     * 读取指定
     *
     * @param list
     * @param selectedId
     * @param labelFieldName
     * @return
     */
    public String getLabel(List<?> list, Object selectedId, String idFieldName, String labelFieldName) {
        if (list == null || list.isEmpty()) {
            return "";
        }
        for (Object one : list) {
            Object id = ReflectUtils.readField(one, idFieldName);
            if (id != null && id.equals(selectedId)) {
                Object val = ReflectUtils.readField(one, labelFieldName);
                return val == null ? "" : val.toString();
            }
        }
        return "";
    }
}
